package cn._2dland.net
{
	import cn._2dland.utils.VideoUtils;
	import cn._2dland.video.StreamInfo;
	
	import flash.events.Event;
	import flash.net.URLRequest;

	public class SinaPartLoader extends PartLoader
	{
		public static const SINA_URL:String = "http://v.iask.com/v_play.php?vid=";

		public function SinaPartLoader(){
			super();
		}

		override public function load(partXml:XML):void {
			super.load(partXml);

			var url:String = SINA_URL + partXml.vid;
			_loader.load(new URLRequest(url));
		}

		override protected function loadCompleteHandler(e:Event):void{
			var sinaXml:XML = new XML(_loader.data);

			if(sinaXml == null || sinaXml.result == "error"){
				_infos.push(new StreamInfo(VideoUtils.DEFAULT_NAME, VideoUtils.DEFAULT_URL, VideoUtils.DEFAULT_DURATION	));
			}
			else{
				var durls:XMLList = sinaXml.durl;
				for(var i:int=0; i<durls.length(); i++){
					var durl:XML = durls[i] as XML;

					var name:String = durls.length() == 1 ? _partName : _partName + "@" + (i + 1).toString();
					var info:StreamInfo = new StreamInfo(name, durl.url, durl.length, _smooth);
					_infos.push(info);
				}
			}
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}